﻿Imports Control_Vuelos_BL
Imports Control_Vuelos_BE
Imports DevExpress.XtraGrid.Views.Grid

Public Class frmPer_Movimiento
    Inherits frmBaseMantenimiento
    Dim oMovimiento As New Movimiento_PersonalBL
    Dim oReservas As New V_ReservaBL
    Dim oPasajero As New PasajeroBL
    Dim Reservas As New V_ReservaBE
    Dim oMaestros As New MaestrosBL

    Private Sub frmPer_Movimiento_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        AddHandler ModMov_Personal1.RepositoryItemButtonEdit1.ButtonClick, AddressOf pr_ClickReserva
        AddHandler ModMov_Personal1.AdvBandedGridView1.ValidatingEditor, AddressOf pr_ValidarGrid
        GetMovimientos()
        ModMov_Personal1.RepositoryItemLookUpEdit1.DataSource = oMaestros.Get_LugarVuelo
        ModMov_Personal1.RepositoryItemLookUpEdit2.DataSource = oMaestros.Get_MotivoExceso()
    End Sub

    Public Sub GetMovimientos()
        ModMov_Personal1.GridControl1.DataSource = oMovimiento.pr_MovimientoPersonal(p_coduser)
    End Sub

    Public Sub pr_ValidarGrid(sender As Object, e As DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs)
        Try
            Dim View As GridView = CType(sender, GridView)
            If View.FocusedColumn.FieldName = "DiasAFavor" Then
                Dim oDiasAFavor As Decimal = Convert.ToDecimal(e.Value)
                Dim oDiasEnContra As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasEnContra"))
                Dim oNoConsiderado As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasNoConsiderado"))
                Dim oDiasTrans As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasTrans"))
                Dim nBase As Decimal = oDiasTrans + oDiasAFavor - oDiasEnContra - oNoConsiderado
                Dim oAdicionales As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasAdicionales"))
                Dim oTotDiaAutorizados As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "TotDiaAutorizados"))
                View.SetRowCellValue(View.FocusedRowHandle, "DiasBase", nBase)
                View.SetRowCellValue(View.FocusedRowHandle, "GozarCalculado", nBase / 2.5)
                View.SetRowCellValue(View.FocusedRowHandle, "GozarRedondeado", Math.Round(nBase / 2.5, 0))
                View.SetRowCellValue(View.FocusedRowHandle, "SaldoDias", (nBase / 2.5) - Math.Round(nBase / 2.5, 0))
                View.SetRowCellValue(View.FocusedRowHandle, "TotDiaCalculados", Math.Round(nBase / 2.5, 0) + oAdicionales)
                View.SetRowCellValue(View.FocusedRowHandle, "DiasPendientes", (Math.Round(nBase / 2.5, 0) + oAdicionales) - oTotDiaAutorizados)
            End If
            If View.FocusedColumn.FieldName = "DiasEnContra" Then
                Dim oDiasEnContra As Decimal = Convert.ToDecimal(e.Value)
                Dim oDiasAFavor As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasAFavor"))
                Dim oNoConsiderado As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasNoConsiderado"))
                Dim oDiasTrans As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasTrans"))
                Dim oAdicionales As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasAdicionales"))
                Dim oTotDiaAutorizados As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "TotDiaAutorizados"))
                Dim nBase As Decimal = oDiasTrans + oDiasAFavor - oDiasEnContra - oNoConsiderado
                View.SetRowCellValue(View.FocusedRowHandle, "DiasBase", nBase)
                View.SetRowCellValue(View.FocusedRowHandle, "GozarCalculado", (nBase) / 2.5)
                View.SetRowCellValue(View.FocusedRowHandle, "GozarRedondeado", Math.Round((nBase) / 2.5, 0))
                View.SetRowCellValue(View.FocusedRowHandle, "SaldoDias", (nBase / 2.5) - Math.Round((nBase) / 2.5, 0))
                View.SetRowCellValue(View.FocusedRowHandle, "TotDiaCalculados", Math.Round(nBase / 2.5, 0) + oAdicionales)
                View.SetRowCellValue(View.FocusedRowHandle, "DiasPendientes", (Math.Round(nBase / 2.5, 0) + oAdicionales) - oTotDiaAutorizados)
            End If
            If View.FocusedColumn.FieldName = "DiasNoConsiderado" Then
                Dim oNoConsiderado As Decimal = Convert.ToDecimal(e.Value)
                Dim oDiasEnContra As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasEnContra"))
                Dim oDiasAFavor As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasAFavor"))
                Dim oDiasTrans As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasTrans"))
                Dim oAdicionales As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasAdicionales"))
                Dim oTotDiaAutorizados As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "TotDiaAutorizados"))
                Dim nBase As Decimal = oDiasTrans + oDiasAFavor - oDiasEnContra - oNoConsiderado
                View.SetRowCellValue(View.FocusedRowHandle, "DiasBase", nBase)
                View.SetRowCellValue(View.FocusedRowHandle, "GozarCalculado", (nBase) / 2.5)
                View.SetRowCellValue(View.FocusedRowHandle, "GozarRedondeado", Math.Round((nBase) / 2.5, 0))
                View.SetRowCellValue(View.FocusedRowHandle, "SaldoDias", (nBase / 2.5) - Math.Round((nBase) / 2.5, 0))
                View.SetRowCellValue(View.FocusedRowHandle, "TotDiaCalculados", Math.Round(nBase / 2.5, 0) + oAdicionales)
                View.SetRowCellValue(View.FocusedRowHandle, "DiasPendientes", (Math.Round(nBase / 2.5, 0) + oAdicionales) - oTotDiaAutorizados)
            End If
            If View.FocusedColumn.FieldName = "FchRetorno" Then
                Dim oFchRetorno As DateTime = Convert.ToDateTime(e.Value)
                Dim oFechaLlegada As DateTime = Convert.ToDateTime(View.GetRowCellValue(View.FocusedRowHandle, "FechaLlegada"))
                Dim nDias As Integer
                nDias = DateDiff(DateInterval.Day, oFechaLlegada, oFchRetorno) + 1
                View.SetRowCellValue(View.FocusedRowHandle, "DiasTrans", nDias)
                Dim oDiasAFavor As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasAFavor"))
                Dim oDiasEnContra As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasEnContra"))
                Dim oNoConsiderado As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasNoConsiderado"))
                Dim oAdicionales As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasAdicionales"))
                Dim oTotDiaAutorizados As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "TotDiaAutorizados"))
                Dim nBase As Decimal = nDias + oDiasAFavor - oDiasEnContra - oNoConsiderado
                View.SetRowCellValue(View.FocusedRowHandle, "DiasBase", nBase)
                View.SetRowCellValue(View.FocusedRowHandle, "GozarCalculado", (nBase) / 2.5)
                View.SetRowCellValue(View.FocusedRowHandle, "GozarRedondeado", Math.Round((nBase) / 2.5, 0))
                View.SetRowCellValue(View.FocusedRowHandle, "SaldoDias", (nBase / 2.5) - Math.Round((nBase) / 2.5, 0))
                View.SetRowCellValue(View.FocusedRowHandle, "TotDiaCalculados", Math.Round(nBase / 2.5, 0) + oAdicionales)
                View.SetRowCellValue(View.FocusedRowHandle, "DiasPendientes", (Math.Round(nBase / 2.5, 0) + oAdicionales) - oTotDiaAutorizados)
            End If

            If View.FocusedColumn.FieldName = "DiasAdicionales" Then
                Dim oAdicionales As Decimal = Convert.ToDecimal(e.Value)
                Dim oNoConsiderado As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasNoConsiderado"))
                Dim oDiasEnContra As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasEnContra"))
                Dim oDiasAFavor As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasAFavor"))
                Dim oDiasTrans As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasTrans"))
                Dim oTotDiaAutorizados As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "TotDiaAutorizados"))
                Dim nBase As Decimal = oDiasTrans + oDiasAFavor - oDiasEnContra - oNoConsiderado
                View.SetRowCellValue(View.FocusedRowHandle, "DiasBase", nBase)
                View.SetRowCellValue(View.FocusedRowHandle, "GozarCalculado", (nBase) / 2.5)
                View.SetRowCellValue(View.FocusedRowHandle, "GozarRedondeado", Math.Round((nBase) / 2.5, 0))
                View.SetRowCellValue(View.FocusedRowHandle, "SaldoDias", (nBase / 2.5) - Math.Round((nBase) / 2.5, 0))
                View.SetRowCellValue(View.FocusedRowHandle, "TotDiaCalculados", Math.Round(nBase / 2.5, 0) + oAdicionales)
                View.SetRowCellValue(View.FocusedRowHandle, "DiasPendientes", (Math.Round(nBase / 2.5, 0) + oAdicionales) - oTotDiaAutorizados)
            End If

            If View.FocusedColumn.FieldName = "TotDiaAutorizados" Then
                Dim oTotDiaAutorizados As Decimal = Convert.ToDecimal(e.Value)
                Dim oNoConsiderado As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasNoConsiderado"))
                Dim oDiasEnContra As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasEnContra"))
                Dim oDiasAFavor As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasAFavor"))
                Dim oDiasTrans As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasTrans"))
                Dim oAdicionales As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasAdicionales"))
                Dim nBase As Decimal = oDiasTrans + oDiasAFavor - oDiasEnContra - oNoConsiderado
                View.SetRowCellValue(View.FocusedRowHandle, "DiasBase", nBase)
                View.SetRowCellValue(View.FocusedRowHandle, "GozarCalculado", (nBase) / 2.5)
                View.SetRowCellValue(View.FocusedRowHandle, "GozarRedondeado", Math.Round((nBase) / 2.5, 0))
                View.SetRowCellValue(View.FocusedRowHandle, "SaldoDias", (nBase / 2.5) - Math.Round((nBase) / 2.5, 0))
                View.SetRowCellValue(View.FocusedRowHandle, "TotDiaCalculados", Math.Round(nBase / 2.5, 0) + oAdicionales)
                View.SetRowCellValue(View.FocusedRowHandle, "DiasPendientes", (Math.Round(nBase / 2.5, 0) + oAdicionales) - oTotDiaAutorizados)
                Dim oFchRetorno As DateTime = Convert.ToDateTime(View.GetRowCellValue(View.FocusedRowHandle, "FchRetorno"))
                Dim oRetProgramado As DateTime = oFchRetorno.AddDays(oTotDiaAutorizados)
                View.SetRowCellValue(View.FocusedRowHandle, "RetProgramado", oRetProgramado)
            End If

            If View.FocusedColumn.FieldName = "RetReal" Then
                Dim oRetReal As DateTime = Convert.ToDateTime(e.Value)
                Dim oRetProgramado As DateTime = Convert.ToDateTime(View.GetRowCellValue(View.FocusedRowHandle, "RetProgramado"))
                Dim oDiasPendientes As Decimal = Convert.ToDecimal(View.GetRowCellValue(View.FocusedRowHandle, "DiasPendientes"))
                Dim nDiasAFavor As Integer = DateDiff(DateInterval.Day, oRetReal, oRetProgramado)
                If nDiasAFavor >= 0 Then
                    View.SetRowCellValue(View.FocusedRowHandle, "DiasAFavorRet", nDiasAFavor)
                Else
                    View.SetRowCellValue(View.FocusedRowHandle, "DiasAFavorRet", 0)
                End If
                Dim nDiasEnContra As Integer = DateDiff(DateInterval.Day, oRetProgramado, oRetReal) + oDiasPendientes
                If nDiasEnContra >= 0 Then
                    View.SetRowCellValue(View.FocusedRowHandle, "DiasEnContraRet", nDiasEnContra)
                Else
                    View.SetRowCellValue(View.FocusedRowHandle, "DiasEnContraRet", 0)
                End If
            End If

        Catch ex As Exception
        End Try

    End Sub

    Public Sub pr_ClickReserva(sender As Object, e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs)
        If ModMov_Personal1.AdvBandedGridView1.RowCount > 0 Then
            Dim Rows As New ArrayList()
            Rows.Add(ModMov_Personal1.AdvBandedGridView1.GetDataRow(ModMov_Personal1.AdvBandedGridView1.GetSelectedRows()(0)))
            Dim Row As DataRow = CType(Rows(0), DataRow)
            If Not Row Is Nothing Then
                If oMovimiento.fn_RecuperarFlagRetorno(Row("CodMovimiento")) Then
                    MsgBox(" Ya se confirmo el Retorno del Pasajero ", MsgBoxStyle.Information)
                    Return
                Else
                    Dim oDialogResult As System.Windows.Forms.DialogResult
                    Dim sfrmChk As New frmChk_ArriboMessage
                    sfrmChk.Text = " Confirmar Reserva del Pasajero "
                    sfrmChk.lbMensaje.Text = " Desea confirmar la reserva de retorno ? "
                    sfrmChk.npMain.Text = " Se procedera a confirmar la reserva del pasajero " & Constants.vbCrLf & Row("AppPasajero") & " " & Row("ApmPasajero") & " " & Row("NomPasajero") & Constants.vbCrLf
                    oDialogResult = sfrmChk.ShowDialog()
                    If oDialogResult = System.Windows.Forms.DialogResult.OK Then
                        If oPasajero.TraerCodigo(Row("NroDocumentoId")) > 0 Then
                            Reservas.FchReserva = Date.Now
                            Reservas.TipPasajero = "1"
                            Reservas.NomContacto = ""
                            Reservas.FchViaje = Row("FchRetorno")
                            Reservas.PsoPasajero = 0
                            Reservas.NroPrioridad = 0
                            Reservas.MotivoViaje = ""
                            Reservas.CodAnalisis = ""
                            Reservas.CodCentroCosto = ""
                            Reservas.PsoEquipajePasajero = 0
                            Reservas.EstadoReserva = "1"
                            Reservas.FlgRequisitos = False
                        End If
                    End If
                End If
            End If
        End If
    End Sub


End Class